package com.lhy.controller;

import com.lhy.domain.Drugs;
import com.lhy.domain.User;
import com.lhy.service.DrugsService;
import com.lhy.utils.Pagination;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 汪
 */
@Controller
public class DrugsController {
    @Autowired
    DrugsService drugsService;
    @RequestMapping("/addDrugs")
    public String addDrugs(HttpServletRequest req,HttpServletResponse resp){
        Map<String, String[]> parameterMap = req.getParameterMap();
        Drugs drugs=new Drugs();
        DateConverter converter = new DateConverter();
        converter.setPattern(new String("yyyy-MM-dd"));
        ConvertUtils.register(converter, Date.class);
        try {
            BeanUtils.populate(drugs,parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        drugsService.addDrugs(drugs);
        return "main";
    }
    @RequestMapping("/deleteDrugs")
    public String deleteDrugs(HttpServletRequest req,HttpServletResponse resp){
        Integer drugsId = Integer.parseInt(req.getParameter("drugsid"));
        drugsService.deleteDrugs(drugsId);
        return "main";
    }
    @RequestMapping("getDrugsId")
    public String getDrugsId(HttpServletRequest req,HttpServletResponse resp){
        Integer drugsId = Integer.parseInt(req.getParameter("drugsid"));
        HttpSession session=req.getSession();
        session.setAttribute("drugsid",drugsId);
        return "ypmodify";
    }
    @RequestMapping("getDrugsId1")
    public String getDrugsId1(HttpServletRequest req,HttpServletResponse resp){
        Integer drugsId = Integer.parseInt(req.getParameter("drugsid"));
        HttpSession session=req.getSession();
        session.setAttribute("drugid",drugsId);
        return "ypcz";
    }
    @RequestMapping("/modifyDrugs")
    public String modifyDrugs(HttpServletRequest req,HttpServletResponse resp){
        Integer drugsid=Integer.parseInt(req.getParameter("drugsid"));
        String price = req.getParameter("price");
        Drugs drugs=new Drugs();
        drugs.setDrugsId(drugsid);
        drugs.setPrice(price);
        drugsService.modifyDrugs(drugs);
        return "main";
    }
    @RequestMapping("/modifyNum")
    public String modifyNum(HttpServletRequest req,HttpServletResponse resp){
        Integer drugsId = Integer.parseInt(req.getParameter("drugsid"));
        Integer num = Integer.parseInt(req.getParameter("num"));
        Drugs drugs=new Drugs();
        drugs.setDrugsId(drugsId);
        drugs.setNum(num);
        System.out.println(drugs);
        drugsService.modifyNum(drugs);
        return "main";
    }
    @RequestMapping("/showSomeDrugs")
    public String showSomeDrugs(Drugs drugs, Pagination<Drugs> page, HttpServletRequest req, HttpServletResponse resp){

        page.setQueryObject(drugs);
        System.out.println(drugs);
        drugsService.showSomeDrugs1(page);
        HttpSession session= req.getSession();
        session.setAttribute("drugses",page);
        return "yplist";
    }
    @RequestMapping("/uploadFile")
    public String uploadFile(HttpServletRequest request, @RequestParam MultipartFile attachmentfile, @RequestParam  Integer drugsId) throws Exception{
        if(attachmentfile!=null && attachmentfile.getSize()>0){
            String oriFilename=attachmentfile.getOriginalFilename();//上传时的文件名
            String[] arr=oriFilename.split("\\.");//abc.pdf,
            String suffix="";
            if(arr.length>=2){
                suffix="."+arr[arr.length-1];
            }
            String fileName=System.currentTimeMillis()+suffix;
            String realPath=request.getServletContext().getRealPath("/WEB-INF/upload/")+"\\"+fileName;
            //写文件
            File file=new File(request.getServletContext().getRealPath("/WEB-INF/upload/"));
            if(!file.exists()){
                file.mkdir();
            }
            file=new File(realPath);
            attachmentfile.transferTo(file);

            Drugs drugs=new Drugs();
            drugs.setDrugsId(drugsId);
            drugs.setOriFilename(oriFilename);
            drugs.setRealFilename(fileName);
            drugsService.updateFilePath(drugs);
        }

        return "redirect:/showDrugs";
    }
    @RequestMapping("/download")
    public void download(Drugs drugs,HttpServletRequest request, HttpServletResponse response) throws Exception{
        List<Drugs> drugs1 = drugsService.showSomeDrugs(drugs);
        drugs= drugs1.get(0);
        //读取文件
        File file=new File(request.getServletContext().getRealPath("/WEB-INF/upload/")+"\\"+drugs.getRealFilename());
        InputStream inputStream=new FileInputStream(file);
        response.setContentType("application/force-download");
        response.setHeader("Content-Disposition","attachment;filename="+drugs.getOriFilename());
        IOUtils.copy(inputStream,response.getOutputStream());
        response.flushBuffer();
        inputStream.close();
    }
}
